package 每日一题OJ;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

/**
 * @author shy_black
 * @date 2019/3/22 13:31
 * @Description: 测试：3 3 1 1 2 3 4 5 6 3 2 1 2 3 4 5 6 2 2 1 1 1 1
 */
public class 洗牌 {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int time = sc.nextInt();//数据组数
        int t = 0;

        ArrayList<Integer> s1 = new ArrayList<Integer>();
        ArrayList<Integer> s2 = new ArrayList<Integer>();

        Stack<Integer> n1 = new Stack<>();//左手
        Stack<Integer> n2 = new Stack<>();//右手

        Stack<Integer> sum = new Stack<>();//结果栈
        while (t < time) {

            int n = sc.nextInt();//有2n张牌
            int k = sc.nextInt();//洗k次牌

            for (int i = 0; i < n; i++) {
                s1.add(sc.nextInt());
            }
            for (int j = 0; j < n; j++) {
                s2.add(sc.nextInt());
            }
            //输入完毕

            //开始循环洗牌
            int count = 0;
            while (count < k) {
                if (count == 0) {
                    for (int i = 0; i < n; i++) {
                        n1.push(s1.get(i));
                    }
                    for (int j = 0; j < n; j++) {
                        n2.push(s2.get(j));
                    }
                }
                //结果栈,牌已洗好
                while (!n1.isEmpty() && !n2.isEmpty()) {
                    sum.push(n2.pop());
                    sum.push(n1.pop());
                }
                count++;
                //要想再次洗牌,需要将结果栈的元素放回n1,n2中
                if (count != k) {
                    for (int i = 0; i < n; i++) {
                        n1.push(sum.pop());
                    }
                    for (int j = 0; j < n; j++) {
                        n2.push(sum.pop());
                    }
                }
            }

            while (!sum.isEmpty()) {
                System.out.print(sum.pop());
                if ((t + 1) != time || !sum.isEmpty())
                    System.out.print(" ");
            }
            s1.clear();
            s2.clear();

            t++;
        }

    }


/**
 * 每次读取一个数之后，算出他经过k次洗牌后的位置，只用一个长度为2n数组用来输出
 * <p>
 * 根据当前数的位置，可以算出经过一次洗牌后的位置
 * 如果当前数小于等于n（即在左手），则他下次出现的位置是 2*当前位置-1
 * 如果当前位置大于n（即在右手）,则他下次出现的位置是 2*（当前位置 - n）
 */

    public static void sloution() {
        Scanner sc = new Scanner(System.in);
        int groups = sc.nextInt();
        while (groups-- > 0) {
            int n = sc.nextInt();
            int k = sc.nextInt();
            int[] res = new int[2 * n];
            for (int i = 0; i < 2 * n; i++) {
                int tmp = i + 1;
                for (int j = 0; j < k; j++) {
                    if (tmp <= n) tmp = 2 * tmp - 1;
                    else tmp = 2 * (tmp - n);
                }
                res[tmp - 1] = sc.nextInt();
            }
            //输出
            if (res.length > 0) System.out.print(res[0]);
            for (int i = 1; i < 2 * n; i++) {
                System.out.print(" " + res[i]);
            }
            System.out.println();
        }
    }
}

